/****************** 节点鼠标Hover 的反应****************/
let timeHover = (function () {
    let div;
    return function (shareId, userId) {
        if (!div) {
            div = document.createElement("div");
            div.className = "choice-wrapper";
            div.shareId = shareId;
            div.userId = userId;
            div.innerHTML =
                '<a class="more-choice" title="更多选择">' +
                '<i class="fa fa-ellipsis-h"></i>' +
                '<div class="more-choice-frame">' +
                '<div class="triangle"></div>' +
                '<div class="triangle-shadow"></div>' +
                '<div class="more-choice-list" title="关闭">关闭</div>' +
                '<div class="more-choice-list" title="举报">举报</div>' +
                '</div>' +
                '</a>' +
                '<a class="detail" title="单独观看">' +
                '<i class="glyphicon glyphicon-fullscreen"></i>' +
                '</a>';
            //更多选择的点击事件
            div.querySelector(".more-choice").onclick = function (event) {
                if (div.querySelector(".more-choice-frame").style.display === "" || div.querySelector(".more-choice-frame").style.display === "none") {
                    div.querySelector(".more-choice-frame").style.display = "block";
                    event.stopPropagation();
                }
                else {
                    div.querySelector(".more-choice-frame").style.display = "none";
                    event.stopPropagation();
                }
            };
            //更多选择放置的显示
            div.querySelector(".more-choice-frame").onmouseover = function () {
                div.querySelector(".more-choice-frame").style.display = "block";
            };
            //详情页的点击事件
            div.querySelector(".detail").onclick = function () {
                window.open(getRootPath_web() + "/share/oneShare?shareId=" + div.shareId + "&userId=" + div.userId, "_self");
            };
            //关闭按钮的点击事件
            div.querySelectorAll(".more-choice-list")[0].onclick = function () {
                let node = div.parentNode.parentNode,
                    pareNode = node.parentNode;
                let data = {
                        shareId: div.shareId,
                        userId: loginUserId,
                        direction: true
                    },
                    url = "/share/hide",
                    method = "POST";
                layer.msg('你确定要关闭这个分享', {
                    time: 10000,
                    btn: ['是的', '取消']
                }, function () {
                    ajax_base(data, url, method, "Json", function () {
                        pareNode.removeChild(node);
                    })
                });

            };
            //举报按钮的点击事件
            div.querySelectorAll(".more-choice-list")[1].onclick = function () {
                let node = div.parentNode.parentNode;
                let pareNode = node.parentNode;
                //重写modelNormal发送框事件
                let modelAbuse = new ModelNormal();
                modelAbuse.ajax_send = function () {
                    let data = {
                            shareId: div.shareId,
                            userId: loginUserId,
                            reason: this.pareNode.querySelector(".textarea-share").value
                        },
                        url = "/share/abuse",
                        method = "POST";
                    return ajax_base(data, url, method, "notJson");
                };
                modelAbuse.setText("分享举报", "举报的原因");
                modelAbuse.setDisplay("举报");
            }
        }
        div.shareId = shareId;
        div.userId = userId;
        return div;
    };
})();
let hover = function () {
    this.nodeList = [];
};
hover.prototype.onHover = function (div, shareId, userId) {
    div.onmouseover = function (event) {
        let point = timeHover(shareId, userId),
            pare = this.querySelector(".share-header"),
            previousSibling = pare.querySelector(".time");
        previousSibling.style.display = 'none';
        if (previousSibling.nextSibling !== point)
            pare.insertBefore(point, previousSibling.nextSibling);
        point.style.display = 'inline-block';
    };
    div.onmouseout = function (event) {
        //oNode用于消除onmouseout的bug
        let oNode = event.toElement || event.relatedTarget;
        if (div.contains(oNode)) {
            return;
        }
        let point = timeHover(shareId, userId),
            previousSibling = this.querySelector(".time");
        previousSibling.style.display = 'inline-block';
        point.style.display = 'none';
        point.querySelector(".more-choice-frame").style.display = "none";
    }
};


/*单个分享的内容顶部*/
let ShowHeader = function (data) {
    this.pointNode = document.createElement("div");
    this.pointNode.className = "share-header";
    //判断是否是转发
    if (data) { //不是
        this.pointNode.innerHTML =
            '<a class="logo"><img src="' + data.author.userLogo.thumbUrl + '"></a>' +
            '<a class="name">' + data.author.userName + '</a>' +
            '<a class="time"><span>' + data.createTime + '</span></a>' +
            '<div class="share-content">' + data.description + '</div>' +
            '<div class="share-header-bottom"></div>';

    } else {//是
    }
};

/*单个分享的内容中部图片*/
let SharePhoto = function (data) {
    this.timerOne = null;
    this.pareNodeWidth = 0;
    this.pointNode = document.createElement("div");
    this.pointNode.className = "share-photo";
    //判断是否是含有多张图
    if (data.images.length === 1) { //单张
        this.pointNode.innerHTML =
            '<img src="' + data.images[0].thumbUrl + '">'
    } else {//多张
        this.pointNode.innerHTML =
            '<div class="share-photo-inner">' +
            '<div class="share-photo-left">' +
            '<i class="fa fa-angle-left"></i>' +
            '</div>' +
            '<div class="share-photo-right">' +
            '<i class="fa fa-angle-right"></i>' +
            '</div>' +
            '<div class="share-photo-wrapper">' +
            '</div>' +
            '</div>';
    }
};
SharePhoto.prototype.setParentNode = function (length) {
    this.pareNodeWidth = length;
};
//多张图片的轮播转动
SharePhoto.prototype.setPhotoRoll = function (data, length) {
    if (data.images.length > 1) {
        this.setParentNode(length);
        let that = this,
            shareImgNode = {
                pareNode: that.pointNode.querySelector(".share-photo-inner"),
                pointNode: that.pointNode.querySelector(".share-photo-wrapper"),
                pointLeft: that.pointNode.querySelector(".share-photo-left"),
                pointRight: that.pointNode.querySelector(".share-photo-right"),
            },
            shareImgNodeLength = this.pareNodeWidth;
        let personResult = new ScrollPhotoComponent(shareImgNode);
        personResult.setOnePhotoNode(that.setOneNode);
        personResult.init(shareImgNodeLength, data.images);
        personResult.resizeToWindow(true);
    }
};
SharePhoto.prototype.setPareWidth = function () {
    let that = this;
    tool.on(window, "resize", function () {
        clearTimeout(that.timerOne);
        that.timerOne = setTimeout(function () {
            let width = that.pointNode.parentNode.parentNode.clientWidth;
            that.pointNode.querySelector(".share-photo-inner").style.width = width + "px";
        });
    })
};
//设置单个节点
SharePhoto.prototype.setOneNode = function (data) {
    return '<div class="share-photo-item item">\n' +
        '<div class="share-photo-item-img"><img src="' + data.thumbUrl + '"></div>\n' +
        '</div>';
};
/*单个分享的内容底部点赞，评论，分享节点*/
let ShareFunction = function (data) {
    this.pointNode = document.createElement("div");
    this.pareShareId = null;
    this.pareUserId = null;
    this.direction = false;
    this.pointNode.className = "share-function";
    if (data) { //没点赞
        this.pointNode.innerHTML =
            this.setPraiseStyle(data) +
            '<a class="add-share"><i class="fa fa-share-alt"></i><span> </span></a>' +
            '<a class="add-comment"><i class="fa fa-comments-o" title="点击查看评论"></i><span> ' + data.totalComment + '</span></a>' +
            '<div class="share-header-bottom"></div>'
    } else {  //有点赞
    }
    this.setPraise();
};
//设置praise样式
ShareFunction.prototype.setPraiseStyle = function (data) {
    if (data.hasPraise) {
        return '<a class="add-Pointer"><i class="fa fa-heart"></i><span> ' + data.totalPraise + '</span></a>'; //实心
    } else {
        return '<a class="add-Pointer"><i class="fa fa-heart-o"></i><span> ' + data.totalPraise + '</span></a>'//空心
    }
};
ShareFunction.prototype.setID = function (shareId, userId) {
    this.pareShareId = shareId;
    this.pareUserId = userId;
};
//点赞方法
ShareFunction.prototype.setPraise = function () {
    let that = this;
    this.pointNode.querySelector(".add-Pointer").onclick = function () {
        let i = this.querySelector("i"),
            span = this.querySelector("span");
        if (i.className === "fa fa-heart-o") {
            that.direction = true;
            that.ajax_praise();
        } else if (this.querySelector("i").className === "fa fa-heart") {
            that.direction = false;
            that.ajax_praise();
        }
    };
};
ShareFunction.prototype.praiseResult = function () {
    let i = this.pointNode.querySelector(".add-Pointer").querySelector("i"),
        span = this.pointNode.querySelector(".add-Pointer").querySelector("span");
    if (i.className === "fa fa-heart-o") {
        i.className = "fa fa-heart";
        let num = span.innerText;
        num = parseInt(num) + 1;
        span.innerText = " " + num;
    } else {
        i.className = "fa fa-heart-o";
        let num = span.innerText;
        num = parseInt(num) - 1;
        span.innerText = " " + num;
    }
};
ShareFunction.prototype.ajax_praise = function () {
    let url = "/share/praise",
        data = {
            shareId: this.pareShareId,
            userId: loginUserId,
            direction: this.direction
        },
        method = "POST",
        that = this;
    ajax_base(data, url, method, "Json").then(function (data) {
        that.praiseResult();
    })
};
ShareFunction.prototype.transmitShare = function () {
    let modelNormal = new ModelNormal();
    this.pointNode.querySelector(".add-share").onclick = function () {
        modelNormal.setText("转发", "你的感受～");
        modelNormal.setDisplay("转发");
    };
};
/*单个分享的内容最底部节点*/
let ShareBottom = function (data) {
    let response = '',
        responseContent = '';
    this.pointNode = document.createElement("div");
    this.pointNode.className = "share-bottom hasComment";
    if (data.totalComment > 0 && data.fastComment) {
        if (data.fastComment && data.fastComment.reply) {
            response = "回复";
            responseContent = data.fastComment.replyUser.userName;
        }
        this.pointNode.innerHTML =
            '<div class="one-comment">' +
            '<div class="share-comment">' +
            '<div class="share-comment-header">' +
            '<a class="logo"><img src="' + data.fastComment.owner.userLogo.thumbUrl + '"></a>' +
            '<a class="name">' + data.fastComment.owner.userName + '</a>' +
            '<a class="response">' + response + '</a>' +
            '<a class="">' + responseContent + '</a>' +
            '<a class="share-commentNode-floor"></a>' +
            '</div>' +
            '<div class="share-comment-middle">' +
            '<a class="share-comment-text">' + data.fastComment.content + '</a>' +
            '</div>' +
            '</div>' +
            '</div>';
    }
};
let _getTheNode = function (object) {
    return object.pointNode;
};
/*单个分享对象*/
let shareNode = function () {
    this.showHeader = '';
    this.sharePhoto = '';
    this.shareFunction = '';
    this.shareBottom = '';
    this.widthOfPare = null;
    this.hov = new hover();//放置出现弹出框事件
};
shareNode.prototype.setWidthOfPare = function (length) {
    this.widthOfPare = length;
};
shareNode.prototype._setModelShare = function (node) {
    let div_addShare = document.createElement("div");
    div_addShare.className = "cont-inner addShare";
    div_addShare.innerHTML =
        '<a class="logoImage"><img src=' + loginImg + '></a>' +
        '<a class="toShare">添加你的分享</a>' +
        '<a class="add"><i class="fa fa-share-alt"></i></a>';

    node.appendChild(div_addShare);
};
shareNode.prototype._setNode = function (data) {
    this.showHeader = new ShowHeader(data);
    this.sharePhoto = new SharePhoto(data);
    this.shareFunction = new ShareFunction(data);
    this.shareBottom = new ShareBottom(data);
    this.shareFunction.setID(data.shareId, data.author.userId);
};
shareNode.prototype.getOneNode = function (data) {
    this._setNode(data);
    let div = document.createElement("div");
    div.className = "cont-inner share";
    if (data.totalComment === 0) {
        div.appendChild(_getTheNode(this.showHeader));
        div.appendChild(_getTheNode(this.sharePhoto));
        div.appendChild(_getTheNode(this.shareFunction));
        this.sharePhoto.setPhotoRoll(data, this.widthOfPare);
        this.shareFunction.transmitShare();
        let commentLi = new TotalComment(data.shareId, div);
        commentLi.show("allShare-zeroComment");
        this.hov.onHover(div, data.shareId, data.author.userId);
    }
    else {
        let oneCom = new oneComment(data.shareId, div);
        div.appendChild(_getTheNode(this.showHeader));
        div.appendChild(_getTheNode(this.sharePhoto));
        div.appendChild(_getTheNode(this.shareFunction));
        div.appendChild(_getTheNode(this.shareBottom));
        this.sharePhoto.setPhotoRoll(data, this.widthOfPare);
        this.shareFunction.transmitShare();
        /* oneCom.showInLoop();*/
        let commentLi = new TotalComment(data.shareId, div);
        commentLi.show("allShare-hasComment");
        this.hov.onHover(div, data.shareId, data.author.userId)
    }
    return div;
};
/*所有分享数组对象*/
let shareArr = function () {
    this.shareNode = new shareNode();
    this.playNode = [getClass("one-cont cont")[0], getClass("two-cont cont")[0], getClass("three-cont cont")[0]]
};
shareArr.prototype.setPlayNode = function (array) {
    this.playNode = array;
};
shareArr.prototype.getPlayNode = function () {
    return this.playNode;
};
shareArr.prototype.showAllNode = function (arrayData, leng, page, addShare) {
    let arr = this.playNode;
    if (this.shareNode.setWidthOfPare)
        this.shareNode.setWidthOfPare(parseInt(this.getPlayNode()[0].style.width));//父组件给子组件传递长度,这个为cont的内容宽度
    for (let j = 0; j < leng; j++) {
        arr[j].style.display = "inline-block";
    }
    for (let j = leng; j < arr.length; j++) {
        arr[j].style.display = "none";
    }
    if (arrayData && (arrayData.length === 1) && leng !== 1) {
        return this.showAllNode(arrayData, 1, page, addShare)
    }

    if (page === 1 && addShare === true) {
        this.shareNode._setModelShare(arr[0]);
    }

    for (let z = 0; z < arrayData.length; z++) {
        let div = this.shareNode.getOneNode(arrayData[z]);
        let tem = z % leng;
        for (let m = 0; m < leng; m++) {
            if (z % leng === m) {
                arr[m].appendChild(div);
            }
        }
    }
};
shareArr.prototype.adjustScreen = function (object, leng) {
    let arr = this.playNode;
    if (this.shareNode.setWidthOfPare)
        this.shareNode.setWidthOfPare(parseInt(this.getPlayNode()[0].style.width));//父组件给子组件传递长度,这个为cont的内容宽度
    for (let j = 0; j < leng; j++) {
        arr[j].style.display = "inline-block";
    }
    for (let j = leng; j < arr.length; j++) {
        arr[j].style.display = "none";
    }
    if (object && object.arrayAllChildrenNode.length === 1 && leng !== 1) {
        return this.adjustScreen(object, 1)
    }
    /*if (object.addShare === true) {
        this.shareNode._setModelShare(arr[0]);
    }*/
    for (let z = 0; z < object.arrayAllChildrenNode.length; z++) {
        let div = object.arrayAllChildrenNode[z];
        let tem = z % leng;
        for (let m = 0; m < leng; m++) {
            if (z % leng === m) {
                arr[m].appendChild(div);
            }
        }
    }
};
/*屏幕对象（方法）*/
let screenAdopt = {
    /*
    * 修改
    */
    adoptScreen: function () {
        let nodeArr = [document.querySelectorAll(".cont")[0],document.querySelectorAll(".cont")[1], document.querySelectorAll(".cont")[2]],
            sharePare = getClass("right-inner")[0],
            length,
            width;
        if (sharePare.offsetWidth > 1450) {
            width = 446;
            length = 3;
        } else if (sharePare.offsetWidth > 953) {
            width = 446;
            length = 2;
        } else {
            width = sharePare.offsetWidth - 24 - 17;
            if (width > 446) {
                width = 446;
            }
            length = 1;
        }
        for (let i = 0; i < nodeArr.length; i++) {
            nodeArr[i].style.width = width + "px";
        }
        return length;
    },
    getMinHeight: function (NodeClassName) {
        let list = getClass(NodeClassName)[0].children;
        let minHeight = 100000000;
        let tempHeight = 0;
        for (let index = 0; index < list.length; index++) {
            if (list[index].style.display !== "none") {
                if (document.getElementsByTagName("body")[0].clientHeight > list[index].lastChild.clientHeight) {
                    tempHeight = document.getElementsByTagName("body")[0].clientHeight;
                } else {
                    tempHeight = list[index].lastChild.clientHeight
                }
                if ((list[index].clientHeight - tempHeight ) < minHeight) {
                    minHeight = list[index].clientHeight - tempHeight;
                }
            }
        }
        return minHeight;
    },
    removeChildrenNodes: function (pName, array) {
        let pareNode = getClass(pName)[0];
        for (let i = 0; i < array.length; i++) {
            this.removeNode(pareNode, array[i]);
        }
    },
    removeNode: function (node, point) {
        for (let i = 0; i < node.children.length; i++) {
            if (node.children[i] === point) {
                node.removeChild(point);
                return;
            } else {
                this.removeNode(node.children[i], point);
            }
        }
    },
    getAllNextChildNode: function (pName) {
        let firstRank = getClass(pName)[0],
            arrayAllParentNode = [],
            arrayAllChildrenNode = [],
            flag = false,
            addShare = false;
        //判断是否有addShare节点 不删除添加节点
        if (firstRank.children[0] && firstRank.children[0].children[0] && firstRank.children[0].children[0].className === "cont-inner addShare") {
            flag = true;
        }
        for (let i = 0; i < firstRank.children[0].children.length; i++) {
            if (flag) {
                for (let j = 0; j < firstRank.children.length; j++) {
                    if (j === 0) {
                        if (firstRank.children[j].children[i + 1])
                            arrayAllChildrenNode.push(firstRank.children[j].children[i + 1])
                    } else {
                        if (firstRank.children[j].children[i])
                            arrayAllChildrenNode.push(firstRank.children[j].children[i])
                    }
                }
            } else {
                for (let j = 0; j < firstRank.children.length; j++) {
                    if (firstRank.children[j].children[i])
                        arrayAllChildrenNode.push(firstRank.children[j].children[i])
                }
            }
        }
        return {
            arrayAllChildrenNode: arrayAllChildrenNode,
            addShare: flag
        }
    }
};

